//! Signal handling system calls
//! SPDX-License-Identifier: MPL-2.0

const std = @import("std");
const SyscallReturn = @import("../return.zig").SyscallReturn;
const SyscallError = @import("../error.zig").SyscallError;
const Context = @import("../context.zig").Context;
const VirtAddr = @import("../context.zig").VirtAddr;

/// Signal numbers
pub const Signal = enum(i32) {
    SIGHUP = 1,
    SIGINT = 2,
    SIGQUIT = 3,
    SIGILL = 4,
    SIGTRAP = 5,
    SIGABRT = 6,
    SIGBUS = 7,
    SIGFPE = 8,
    SIGKILL = 9,
    SIGUSR1 = 10,
    SIGSEGV = 11,
    SIGUSR2 = 12,
    SIGPIPE = 13,
    SIGALRM = 14,
    SIGTERM = 15,
    SIGSTKFLT = 16,
    SIGCHLD = 17,
    SIGCONT = 18,
    SIGSTOP = 19,
    SIGTSTP = 20,
    SIGTTIN = 21,
    SIGTTOU = 22,
    SIGURG = 23,
    SIGXCPU = 24,
    SIGXFSZ = 25,
    SIGVTALRM = 26,
    SIGPROF = 27,
    SIGWINCH = 28,
    SIGIO = 29,
    SIGPWR = 30,
    SIGSYS = 31,
    _,
};

/// Signal action structure
pub const SigAction = extern struct {
    sa_handler: VirtAddr,
    sa_flags: u64,
    sa_restorer: VirtAddr,
    sa_mask: u64,
};

/// Signal set type
pub const SigSet = u64;

/// How to change signal mask
pub const SigHow = enum(i32) {
    SIG_BLOCK = 0,
    SIG_UNBLOCK = 1,
    SIG_SETMASK = 2,
    _,
};

/// Set signal action
pub fn sys_rt_sigaction(
    sig: i32,
    act: VirtAddr,
    oldact: VirtAddr,
    sigsetsize: usize,
    ctx: *Context,
) SyscallReturn {
    _ = sig;
    _ = act;
    _ = oldact;
    _ = sigsetsize;
    _ = ctx;
    // TODO: Implement signal action setting
    return SyscallReturn.error_return(SyscallError.ENOSYS);
}

/// Change signal mask
pub fn sys_rt_sigprocmask(
    how: i32,
    set: VirtAddr,
    oldset: VirtAddr,
    sigsetsize: usize,
    ctx: *Context,
) SyscallReturn {
    _ = how;
    _ = set;
    _ = oldset;
    _ = sigsetsize;
    _ = ctx;
    // TODO: Implement signal mask manipulation
    return SyscallReturn.error_return(SyscallError.ENOSYS);
}

/// Return from signal handler
pub fn sys_rt_sigreturn(ctx: *Context) SyscallReturn {
    _ = ctx;
    // TODO: Implement signal return
    // This would restore the context from before signal delivery
    return SyscallReturn.error_return(SyscallError.ENOSYS);
}